﻿@{
string selected = Request.QueryString["selected"];
string from = Request.QueryString["from"];

string connString =  System.Configuration.ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
MySql.Data.MySqlClient.MySqlConnection connection = new MySql.Data.MySqlClient.MySqlConnection(connString);


string selectQueryString = "SELECT * FROM "+from+" WHERE name='"+selected+"';";

MySql.Data.MySqlClient.MySqlCommand command = new MySql.Data.MySqlClient.MySqlCommand(selectQueryString, connection);
}

<!DOCTYPE html>

<html lang="pl">
    <head>
        <meta charset="utf-8" />
        <title>Layout</title>
        <link href="Images/favicon.ico" rel="shortcut icon" type="image/x-icon" />
        <link rel="stylesheet" type="text/css" href="Shared/styles.css">
    </head>

    <body>
        <!-- Header -->
        @switch(from)
        {
            case "hosts":
            {
                <p style="font-family: 'Segoe UI'; font-size: 45px; color: #fff; padding: 0px; margin: 0px 25px 0px 10px">Stacja robocza</p>
                break;    
            }
            case "subnets":
            {
                <p style="font-family: 'Segoe UI'; font-size: 45px; color: #fff; padding: 0px; margin: 0px 25px 0px 10px">Podsieć</p>
                break;    
            }
            case "servers":
            {
                <p style="font-family: 'Segoe UI'; font-size: 45px; color: #fff; padding: 0px; margin: 0px 25px 0px 10px">Serwer</p>
                break;    
            }
        }

        <!-- Content -->
        <div>
            <p style="font-family: 'Segoe UI'; font-size: 30px; color: #808080; padding: 0px; margin: 0px 25px 0px 10px">@selected</p>

            <!-- Edit -->
            <div style="width: 450px; height: auto; position: absolute; margin-left: 10px">
                @switch(from)
                {
                    case "hosts":
                    {
                        MySql.Data.MySqlClient.MySqlDataReader r = null;
                        MySql.Data.MySqlClient.MySqlCommand cmd = null;
                        System.Data.IDataRecord summary = null;

                        try
                        {
                            connection.Open();
                            cmd = new MySql.Data.MySqlClient.MySqlCommand("SELECT * FROM hosts WHERE name='"+selected+"';", connection);
                            r = cmd.ExecuteReader();
                            r.Read();
                            summary = (System.Data.IDataRecord)r;

                            string name, hardware_ethernet, fixed_address, subnet;

                            name = summary.GetString(0);
                            hardware_ethernet = summary.GetString(1);
                            fixed_address = summary.GetString(2);
                            subnet = summary.GetString(3);

                            r.Close();
                            
                            <p style="font-family: 'Segoe UI'; font-size: 20px; color: #808080; margin: 0px 0px 20px 0px">Edycja podsieci</p>
                            <form action="Edit.cshtml" style="color: #fff; font-family: 'Segoe UI'" method="post">
                                <a style="width: 205px; display: inline-block; margin-bottom: 7px">Nazwa:</a><input style="width: 230px" type="text" name="name" value="@name"><br>
                                <a style="width: 205px; display: inline-block; margin-bottom: 7px">Adres fizyczny:</a><input style="width: 230px" type="text" name="hardware_ethernet" value="@hardware_ethernet"><br>
                                <a style="width: 205px; display: inline-block; margin-bottom: 7px">Adres logiczny:</a><input style="width: 230px" type="text" name="fixed_address" value="@fixed_address"><br>
                                <a style="width: 201px; display: inline-block; margin-bottom: 7px">Podsiec:</a>

                                @{
                                    MySql.Data.MySqlClient.MySqlCommand com = null;
                                    MySql.Data.MySqlClient.MySqlDataReader rd = null;
                                    System.Data.IDataRecord rec = null;

                                    try
                                    {
	                                    com = new MySql.Data.MySqlClient.MySqlCommand("SELECT id,name FROM subnets;", connection);
	                                    rd = com.ExecuteReader();

                                        List<String[]> tab = new List<String[]>();

                                        <select style="width: 234px" name="subnet">
                                            @{
                                                while(rd.Read())
                                                {
                                                    rec = (System.Data.IDataRecord)rd;
                                                    if(rec.GetString(0)==subnet)
                                                    {   
                                                        <option value="@subnet">@rec.GetString(1)</option>
                                                    }
                                                    else
                                                    {
                                                        String[] tmp = new String[2];
                                                        tmp[0] = rec.GetString(0);
                                                        tmp[1] = rec.GetString(1);

                                                        tab.Add(tmp);
                                                    }
                                                }

                                                foreach(String[] item in tab)
                                                {
                                                    <option value="@item[0]">@item[1]</option>
                                                }
                                            }
                                        </select>
                                    }
                                    catch(Exception ex)
                                    {
	                                    Response.Write(ex.Message);
                                    }
                                    finally
                                    {
	                                    rd.Close();
                                    }
                                }
                                
                                <input type="hidden" value="hosts" name="from">
                                <input type="hidden" value="@selected" name="selected">
                                <input style="width: 100px; margin-top: 8px; margin-left: 339px" type="submit" value="Edytuj">
                            </form>
                        }
                        catch(Exception ex)
                        {
                            Response.Write(ex.Message);
                        }
                        finally
                        {
                            connection.Close();
                        }
                        break;    
                    }
                    case "subnets":
                    {
                        MySql.Data.MySqlClient.MySqlDataReader r = null;
                        MySql.Data.MySqlClient.MySqlCommand cmd = null;
                        System.Data.IDataRecord summary = null;

                        try
                        {
                            connection.Open();
                            cmd = new MySql.Data.MySqlClient.MySqlCommand("SELECT * FROM subnets WHERE name='"+selected+"';", connection);
                            r = cmd.ExecuteReader();
                            r.Read();
                            summary = (System.Data.IDataRecord)r;

                            string name, address, netmask, broadcast_address, routers, server, building, _range;

                            name = summary.GetString(8);
                            address = summary.GetString(1);
                            netmask = summary.GetString(2);
                            broadcast_address = summary.GetString(4);
                            routers = summary.GetString(5);
                            server = summary.GetString(6);
                            building = summary.GetString(7);
                            _range = summary.GetString(3);

                            r.Close();
                            
                            <p style="font-family: 'Segoe UI'; font-size: 20px; color: #808080; margin: 0px 0px 20px 0px">Edycja podsieci</p>
                            <form action="Edit.cshtml" style="color: #fff; font-family: 'Segoe UI'" method="post">
                                <a style="width: 205px; display: inline-block; margin-bottom: 7px">Nazwa:</a><input style="width: 230px" type="text" name="name" value="@name"><br>
                                <a style="width: 205px; display: inline-block; margin-bottom: 7px">Adres:</a><input style="width: 230px" type="text" name="address" value="@address"><br>
                                <a style="width: 205px; display: inline-block; margin-bottom: 7px">Maska podsieci:</a><input style="width: 230px" type="text" name="netmask" value="@netmask"><br>
                                <a style="width: 205px; display: inline-block; margin-bottom: 7px">Adres rozgłoszeniowy:</a><input style="width: 230px" type="text" name="broadcast_address" value="@broadcast_address"><br>
                                <a style="width: 205px; display: inline-block; margin-bottom: 7px">Router:</a><input style="width: 230px" type="text" name="routers" value="@routers"><br>

                                <a style="width: 201px; display: inline-block; margin-bottom: 7px">Serwer:</a>
                                @{
                                    MySql.Data.MySqlClient.MySqlCommand com = null;
                                    MySql.Data.MySqlClient.MySqlDataReader rd = null;
                                    System.Data.IDataRecord rec = null;

                                    try
                                    {
	                                    com = new MySql.Data.MySqlClient.MySqlCommand("SELECT id,name FROM servers;", connection);
	                                    rd = com.ExecuteReader();

                                        List<String[]> tab = new List<String[]>();

                                        <select style="width: 234px" name="server">
                                            @{
                                                while(rd.Read())
                                                {
                                                    rec = (System.Data.IDataRecord)rd;
                                                    if(rec.GetString(0)==server)
                                                    {   
                                                        <option value="@server">@rec.GetString(1)</option>
                                                    }
                                                    else
                                                    {
                                                        String[] tmp = new String[2];
                                                        tmp[0] = rec.GetString(0);
                                                        tmp[1] = rec.GetString(1);

                                                        tab.Add(tmp);
                                                    }
                                                }

                                                foreach(String[] item in tab)
                                                {
                                                    <option value="@item[0]">@item[1]</option>
                                                }
                                            }
                                        </select>
                                    }
                                    catch(Exception ex)
                                    {
	                                    Response.Write(ex.Message);
                                    }
                                    finally
                                    {
	                                    rd.Close();
                                    }
                                }
                                
                                <a style="width: 205px; display: inline-block; margin-bottom: 7px">Budynek:</a><input style="width: 230px" type="text" name="building" value="@building"><br>
                                <a style="width: 205px; display: inline-block; margin-bottom: 7px">Zakres:</a><input style="width: 230px" type="text" name="_range" value="@_range"><br>
                                <input type="hidden" value="subnets" name="from">
                                <input type="hidden" value="@selected" name="selected">
                                <input style="width: 100px; margin-top: 8px; margin-left: 339px" type="submit" value="Edytuj">
                            </form>
                        }
                        catch(Exception ex)
                        {
                            Response.Write(ex.Message);
                        }
                        finally
                        {
                            connection.Close();
                        }
                        break;    
                    }
                    case "servers":
                    {
                        string select = "SELECT * FROM servers WHERE name='"+selected+"';";

                        connection.Open();

                        MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand(select, connection);
                        MySql.Data.MySqlClient.MySqlDataReader r = cmd.ExecuteReader();
                        r.Read();
                        
                        System.Data.IDataRecord summary = (System.Data.IDataRecord)r;

                        <p style="font-family: 'Segoe UI'; font-size: 20px; color: #808080; margin: 0px 0px 20px 0px">Edycja serwera</p>
                        <form action="Edit.cshtml" style="color: #fff; font-family: 'Segoe UI'" method="post">
                            <a style="width: 205px; display: inline-block; margin-bottom: 7px">Nazwa:</a><input style="width: 230px" type="text" name="name" value="@summary.GetString(5)"><br>
                            <a style="width: 205px; display: inline-block; margin-bottom: 7px">DNS:</a><input style="width: 230px" type="text" name="domain_name_servers" value="@summary.GetString(1)"><br>
                            <a style="width: 205px; display: inline-block; margin-bottom: 7px">Domyślny czas dzierżawy:</a><input style="width: 230px" type="text" name="default_lease_time" value="@summary.GetString(2)"><br>
                            <a style="width: 205px; display: inline-block; margin-bottom: 7px">Maksymalny czas dzierżawy:</a><input style="width: 230px" type="text" name="max_lease_time" value="@summary.GetString(3)"><br>
                            <a style="width: 205px; display: inline-block; margin-bottom: 7px">Autorytatywny:</a><input style="width: 230px" type="text" name="authoritative" value="@summary.GetString(4)"><br>
                            <a style="width: 205px; display: inline-block; margin-bottom: 7px">Log obiektu:</a><input style="width: 230px" type="text" name="log_facility" value="@summary.GetString(6)"><br>
                            <input type="hidden" value="servers" name="from">
                            <input type="hidden" value="@selected" name="selected">
                            <input style="width: 100px; margin-top: 8px; margin-left: 339px" type="submit" value="Edytuj">
                        </form>
                        
                        r.Close();
                        connection.Close();
                        break;    
                    }
                }
            </div>
            <!-- List -->
            <div style="width: auto; height: auto; position: absolute; left: 460px">
            @{
                switch(from)
                {
                    case "subnets":
                    {
                        <p style="font-family: 'Segoe UI'; font-size: 20px; color: #808080; margin: 0px 0px 20px 0px">Lista hostów w podsieci @selected</p>

                        string id = "";

                        string select = "SELECT id FROM subnets WHERE name='"+selected+"';";

                        connection.Open();

                        command = new MySql.Data.MySqlClient.MySqlCommand(select, connection);
                        MySql.Data.MySqlClient.MySqlDataReader read = command.ExecuteReader();
                        read.Read();
                        System.Data.IDataRecord summary = (System.Data.IDataRecord)read;
                        id = summary.GetString(0);
                        string selectQuery = "SELECT name FROM hosts WHERE subnet="+id+";"; 
                        read.Close();

                        command = new MySql.Data.MySqlClient.MySqlCommand(selectQuery, connection);
                        read = command.ExecuteReader();
                        
                        <div style="overflow-x: auto; overflow-y: auto; overflow: scroll; height: 750px">
                            @while (read.Read())
                            {
                                summary = (System.Data.IDataRecord)read;
                                string name = summary.GetString(0);
                                string url = "Selected.cshtml?selected="+name+"&from=hosts";
                
                                if(name.Length <= 12)
                                {
                                    <a href="@url">
                                        <div class="tile">
                                            <p>@summary.GetString(0)</p>
                                        </div>
                                    </a>
                                }
                                else
                                {
                                    string tmp = name.Substring(0,12)+"...";
                                    <a href="@url">
                                        <div class="tile">
                                            <p>@tmp</p>
                                        </div>
                                    </a>
                                }
                            }
                        </div>

                        read.Close();
                        break;
                    }
                    case "servers":
                    {
                        <p style="font-family: 'Segoe UI'; font-size: 20px; color: #808080; margin: 0px 0px 20px 0px">Lista podsieci na serwerze @selected</p>

                        string id = "";

                        string select = "SELECT id FROM servers WHERE name='"+selected+"';";

                        connection.Open();

                        command = new MySql.Data.MySqlClient.MySqlCommand(select, connection);
                        MySql.Data.MySqlClient.MySqlDataReader read = command.ExecuteReader();
                        read.Read();
                        System.Data.IDataRecord summary = (System.Data.IDataRecord)read;
                        id = summary.GetString(0);
                        string selectQuery = "SELECT name FROM subnets WHERE server="+id+";"; 
                        read.Close();

                        command = new MySql.Data.MySqlClient.MySqlCommand(selectQuery, connection);
                        read = command.ExecuteReader();
                        while (read.Read())
                        {
                            summary = (System.Data.IDataRecord)read;
                            string name = summary.GetString(0);
                            string url = "Selected.cshtml?selected="+name+"&from=subnets";
                
                            if(name.Length <= 12)
                            {
                                <a href="@url">
                                    <div class="tile">
                                        <p>@summary.GetString(0)</p>
                                    </div>
                                </a>
                            }
                            else
                            {
                                string tmp = name.Substring(0,12)+"...";
                                <a href="@url">
                                    <div class="tile">
                                        <p>@tmp</p>
                                    </div>
                                </a>
                            }
                        }

                        read.Close();
                        break;
                    }
                }
                connection.Close();
            }
            </div>
        </div>
        <!-- Menu -->
        <div style="height: 30px; width: 100%; background-color: #0094ff; position: fixed; bottom: 0px; padding-left: 10px">
            <a href="Search.cshtml?from=@from"><div class="menu_item"><p style="color: #fff; margin: 3px; font-family: 'Segoe UI'">Wyszukaj</p></div></a>
            <a href="Delete.cshtml?from=@from&selected=@selected"><div class="menu_item"><p style="color: #fff; margin: 3px; font-family: 'Segoe UI'">Usuń</p></div></a>
            @{
                if(from=="servers")
                {
                    <a href="Export.cshtml?selected=@selected"><div class="menu_item"><p style="color: #fff; margin: 3px; font-family: 'Segoe UI'">Eksportuj</p></div></a>
                }
            }
        </div>
    </body>
</html>